// pages/editView/recruit/recruit.js
let network = require('../../../utils/requestManager.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    RecruitId : "",
    icon_industry:true,
    icon_job:true,
    icon_zhize:true,
    icon_zige:true,
    icon_people:true,
    icon_min_money:true,
    icon_max_money:true,
    icon_fuli:true,
    icon_min_age: true,
    icon_max_age:true,
    icon_min_age:true,
    icon_max_age:true,
    hidden_industry:true,
    hidden_job:true,
    hidden_welfare:true,
    industry_val:"",
    industry_id:"",
    job_val:"",
    duty_val:"",
    qualifications_val:"",
    welfare_val:"",
    industrys:[],
    jobs: [],
    welfares: [],
    choose_welfare_arr:[],
    other_industry_modal:true,//其他行业输入框
    other_job_modal:true,//自定义职位模态框
    recruit_textarea:false,//textarea显示/隐藏
    other_industry_val: "",//其他行业内容
    other_job_val:"",//其他职位内容
  },

  /**
   * 生命周期函数--监听页面加载
   * 选择行业数组添加其他元素
   * 选择职位添加其他
   */
  onLoad: function (options) {
    console.log(options)
    let that = this;
    let recruit_id = options.recruitment_id
    if (recruit_id == undefined || recruit_id == ""){
      recruit_id = ""
    }else{
      that.setData({
        RecruitId: recruit_id
      })
      getRecruitData(that,recruit_id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 选择行业获得焦点
   * 行业modal显示
   * **/
  show_industry_modal:function(){
    //选择行业添加其他
    let that = this;
    let industry_name = that.data.industry_val//行业名称
    network.getRequest('recruitment/industriy/list?name=' + industry_name,{},function(res){
      console.log(res)
      let industry_arr = res.detail;
      let industry_other = {
        name: "其他",
        id: "other"
      };
      industry_arr.push(industry_other);
      that.setData({
        hidden_industry: false,
        industrys: industry_arr,
        recruit_textarea:true,//隐藏textarea
      })
    })
    
  },


  /**
   * 选择职位获得焦点
   * 职位modal显示
   * **/
  show_job_modal: function () {
    let that = this;
    let industry_name = that.data.industry_val//行业名称
    let job_val = that.data.job_val//职位
    if(industry_name == ""){
      wx.showToast({
        title: '请先选择行业',
        icon:"none",
      })
      return
    }else{
      network.getRequest('recruitment/vocation/list?i_name=' + industry_name + '&name=' + job_val, {}, function (res) {
        //选择职位添加自定义
        console.log(res)
        let job_arr = res.detail;
        let job_other = {
          name: "自定义",
          id: "other",
          require: "",
          obligation: "",
          status: false
        };
        job_arr.push(job_other);
        that.setData({
          hidden_job: false,
          jobs: job_arr,
          recruit_textarea: true,//隐藏textarea
        })
      })
    }
  },

  /**
   * 点击行业按钮
   * 行业value=点击的name
   * 点击添加样式
   * 判断点击其他，id = other
   * **/
  choose_industry:function(e){
    let that = this;
    console.log(e)
    let this_checked = e.currentTarget.dataset.id
    let this_name = e.currentTarget.dataset.name
    that.setData({
      industry_val:this_name,
      industry_id: this_checked
    })
    let industrys_arr = this.data.industrys//获取Json数组
    for (var i = 0; i < industrys_arr.length; i++) {
      if (industrys_arr[i].id == this_checked) {
        industrys_arr[i].checked = true;//当前点击的位置为true即选中
      }else {
        industrys_arr[i].checked = false;//其他的位置为false
      }
    }
    that.setData({
      industrys: industrys_arr
    })
    /**
     * 点击其他
     * 隐藏行业模态框，弹出输入框
     * 填写其他行业名称
     * **/
    if(this_checked == "other"){
      console.log("点击了其他")
      that.setData({
        hidden_industry:true,
        other_industry_modal:false,
        industry_val:""
      })
    }
  },

  /**
   * 输入其他行业事件
   * 把输入框内的值传递给”选择行业“的输入框
   * **/
  other_industry_input:function(e){
    console.log(e)
    let that = this;
    that.setData({
      industry_val:e.detail.value,
      other_industry_val: e.detail.value//其他行业input输入框值
    })
  },

  /**
   * 其他行业输入完成
   * 点击确定，关闭模态框
   * **/
  confirm_other_industry:function(){
    let that = this;
    if (that.data.other_industry_val == ""){
      wx.showToast({
        title: '行业不能为空',
        icon:"none"
      })
      return
    }else{
      that.setData({
        industry_val: that.data.other_industry_val,
        other_industry_modal: true,
        icon_industry: false,
        recruit_textarea: false,//显示textarea
      })
    }
  },
  /**
   * 选择职位按钮
   * 行业value=点击的name
   * 点击添加样式
   * 判断点击其他，id = other
   * **/
  choose_job: function (e) {
    let that = this;
    console.log(e)
    let this_checked = e.currentTarget.dataset.id
    let this_name = e.currentTarget.dataset.name
    let this_obligation = e.currentTarget.dataset.obligation
    let this_require = e.currentTarget.dataset.require

    console.log(this_obligation + this_require)
    that.setData({
      job_val: this_name,
      duty_val: this_obligation,//岗位职责
      qualifications_val: this_require//任职资格
    })
    let job_arr = this.data.jobs//获取Json数组
    for (var i = 0; i < job_arr.length; i++) {
      if (job_arr[i].id == this_checked) {
        job_arr[i].checked = true;//当前点击的位置为true即选中
      }
      else {
        job_arr[i].checked = false;//其他的位置为false
      }
    }
    that.setData({
      jobs: job_arr
    })
    //点击其他
    if (this_checked == "other") {
      console.log("点击了其他")
      that.setData({
        hidden_job:true,
        other_job_modal:false,
        job_val: "",//职位名称制空
      })
    }
  },

  /**
   * 输入其他职位事件
   * 赋值给”选择职位“
   * **/
  other_job_input:function(e){
    let that = this;
    that.setData({
      job_val: e.detail.value,
      other_job_val: e.detail.value,
    })
  },

  /**
   * 输入其他职位完成
   * 点击确定关闭模态框
   * **/
  confirm_other_job:function(){
    let that = this;
    if (that.data.other_job_val == ""){
      wx.showToast({
        title: '职位不能为空',
        icon:"none"
      })
    }else{
      that.setData({
        other_job_modal:true,
        icon_job:false,
        recruit_textarea: false,//显示textarea
        job_val: that.data.other_job_val
      })
    }
  },

  /**
   * 选择福利获得焦点
   * 福利modal显示
   * **/
  show_welfare_modal: function () {
    let that = this;
    let choose_history_arr = that.data.choose_welfare_arr;//选择历史福利
    console.log(that.data.choose_welfare_arr)
    let history_index = []
    let history_item = {}
    //获取已选择的福利下标并存在数组
    for(let h = 0;h < choose_history_arr.length;h++){
      history_index.push(choose_history_arr[h].index)
    }

    network.getRequest('recruitment/welfare/list', {}, function (res) {
      //选择职位添加自定义
      console.log(res)
      let welfares_arr = res.detail;
      //拿到历史对应的下标，修改请求的下标的status
      for (let i = 0; i < welfares_arr.length; i++) {
        welfares_arr[i]["index"] = i;
        for(let h = 0;h<history_index.length;h++){
          if(i == history_index[h]){
            welfares_arr[i].status = true
          }
        }
      }
      console.log(welfares_arr)
      that.setData({
        hidden_welfare: false,
        welfares: welfares_arr,
        recruit_textarea: true,//隐藏textarea
      })
    })

  },

  /**
   * 点击福利按钮
   * 行业value=点击的name
   * 点击添加样式
   * **/
  choose_welfare: function (e) {
    console.log(e)
    let that = this;
    // let welfares = that.data.welfares;//请求的所有福利列表
    let welfare_arr = that.data.welfares//获取Json数组
    let choose_welfare_arr = []//选中福利数组
    let check_arr = e.detail.value//获取选中的值
    console.log(check_arr)
    console.log(welfare_arr)

    for (let i = 0; i < welfare_arr.length; i++) {
      let isStatus=false;
      for (let j = 0; j < check_arr.length;j++){
        if (i == check_arr[j]){
          isStatus=true;
        }
      }
      if (isStatus) {
        welfare_arr[i].status = true;
        let welfare_name = {};
        welfare_name.status = true;//选中福利状态
        welfare_name.id = welfare_arr[i].id;//福利id
        welfare_name.index = welfare_arr[i].index;//
        welfare_name.name = welfare_arr[i].name;
        choose_welfare_arr.push(welfare_name)
      }else{
        welfare_arr[i].status = false;
      }
    }
    console.log(welfare_arr)

    that.setData({
      welfares: welfare_arr,
      choose_welfare_arr: choose_welfare_arr//选中的福利赋值
    })

    return
  },

  /**
   * 选择行业点击确认
   * 关闭modal
   * **/
  confirm_industry:function(){
    let that = this;
    let industrys_name = that.data.industry_val//选中的行业名称
    console.log(industrys_name)
    if(industrys_name == undefined || industrys_name == ""){
      wx.showToast({
        title: '请选择行业',
        icon:"none"
      })
      return
    }
    that.setData({
      icon_industry: false,
      hidden_industry:true,
      recruit_textarea: false,//显示textarea
    })
  },

  /**
   * 选择职位点击确认
   * 关闭modal
   * **/
  confirm_job: function () {
    let that = this;
    let job_name = that.data.job_val//职位名称
    if (job_name == undefined || job_name == ""){
      wx.showToast({
        title: '请选择职位',
        icon: "none"
      })
      return
    }
    that.setData({
      icon_job: false,
      hidden_job: true,
      recruit_textarea:false,//显示textarea
    })
  },

  /**
   * 选择福利点击确认
   * 关闭modal
   * **/
  confirm_welfare: function () {
    let that = this;
    let welfare_arr = that.data.choose_welfare_arr.length//选择的福利
    if (welfare_arr == 0) {
      wx.showToast({
        title: '请选择福利',
        icon: "none"
      })
      return
    }

    //最多选择6个
    if(welfare_arr > 6){
      wx.showToast({
        title: '最多能选择6个',
        icon:"none"
      })
      return
    }
    that.setData({
      icon_fuli: false,
      hidden_welfare: true,
      recruit_textarea: false,//显示textarea
    })
  },

  /**
   * 岗位职责失去焦点
   * **/
  recruit_zhize_out: function (e) {
    let that = this;
    let recruit_zhize = e.detail.value;
    if (recruit_zhize === "" || recruit_zhize === null || recruit_zhize === "undefined") {
      that.setData({
        icon_zhize: true
      })
    } else {
      that.setData({
        icon_zhize: false
      })
    }
  },

  /**
   * 任职资格失去焦点
   * **/
  recruit_zige_out: function (e) {
    let that = this;
    let recruit_zige = e.detail.value;
    if (recruit_zige === "" || recruit_zige === null || recruit_zige === "undefined") {
      that.setData({
        icon_zige: true
      })
    } else {
      that.setData({
        icon_zige: false
      })
    }
  },

  /**
   * 招聘人数失去焦点
   * **/
  recruit_people_out: function (e) {
    let that = this;
    let recruit_people = e.detail.value;
    if (recruit_people === "" || recruit_people === null || recruit_people === "undefined") {
      that.setData({
        icon_people: true
      })
    } else {
      that.setData({
        icon_people: false
      })
    }
  },

  /**
   * 最低月薪失去焦点
   * **/
  recruit_min_money_out: function (e) {
    let that = this;
    let recruit_min_money = e.detail.value;
    if (recruit_min_money === "" || recruit_min_money === null || recruit_min_money === "undefined") {
      that.setData({
        icon_min_money: true
      })
    } else {
      that.setData({
        icon_min_money: false
      })
    }
  },

  /**
   * 最高月薪失去焦点
   * **/
  recruit_max_money_out: function (e) {
    let that = this;
    let recruit_max_money = e.detail.value;
    if (recruit_max_money === "" || recruit_max_money === null || recruit_max_money === "undefined") {
      that.setData({
        icon_max_money: true
      })
    } else {
      that.setData({
        icon_max_money: false
      })
    }
  },

  /**
   * 招聘最小年龄失去焦点
   * **/
  recruite_min_age_out: function (e) {
    let that = this;
    let recruit_age = e.detail.value;
    if (recruit_age === "" || recruit_age === null || recruit_age === "undefined") {
      that.setData({
        icon_min_age: true
      })
    } else {
      that.setData({
        icon_min_age: false
      })
    }
  },

  /**
   * 招聘最大年龄失去焦点
   * **/
  recruite_max_age_out: function (e) {
    let that = this;
    let recruit_age = e.detail.value;
    if (recruit_age === "" || recruit_age === null || recruit_age === "undefined") {
      that.setData({
        icon_max_age: true
      })
    } else {
      that.setData({
        icon_max_age: false
      })
    }
  },

  /**
   * 点击保存
   * form_data为form表单的name
   * **/
  activity_submit:function(e){
    let that = this
    let form_data = e.detail.value;
    let industry_val = form_data.industryName//行业名称
    let job_val = form_data.vocation//职位
    let obligation = form_data.obligation//岗位
    let tenure = form_data.require//任职资格
    let people = form_data.number//人数
    let min_money = form_data.min_money * 100//最低月薪
    let max_money = form_data.max_money * 100//最高月薪
    let min_age = form_data.min_age//最低年龄
    let max_age = form_data.max_age//最高年龄
    let welfare_val = JSON.stringify(that.data.choose_welfare_arr)//福利

    if (that.data.choose_welfare_arr.length == 0) {
      wx.showToast({
        title: '选择福利不能为空',
        icon: "none"
      })
      return
    }

    if (industry_val == ""){
      wx.showToast({
        title: '选择行业不能为空',
        icon:"none"
      })
      return
    }
    if (job_val == "") {
      wx.showToast({
        title: '选择职位不能为空',
        icon: "none"
      })
      return
    }
    if (obligation == "") {
      wx.showToast({
        title: '岗位职责不能为空',
        icon: "none"
      })
      return
    }
    if (tenure == "") {
      wx.showToast({
        title: '任职资格不能为空',
        icon: "none"
      })
      return
    }

    if (people == "") {
      wx.showToast({
        title: '招聘人数不能为空',
        icon: "none"
      })
      return
    }
    if (min_money == "") {
      wx.showToast({
        title: '最低月薪不能为空',
        icon: "none"
      })
      return
    }
    if (max_money == "") {
      wx.showToast({
        title: '最高月薪不能为空',
        icon: "none"
      })
      return
    }
    if (min_age == "") {
      wx.showToast({
        title: '最小年龄不能为空',
        icon: "none"
      })
      return
    }
    if (max_age == "") {
      wx.showToast({
        title: '最大年龄不能为空',
        icon: "none"
      })
      return
    }
    if (welfare_val.length == 0){
      wx.showToast({
        title: '选择福利不能为空',
        icon: "none"
      })
      return
    }

    console.log(e)
    console.log(this.data.welfares)
    let shop_id = network.shopParams().shop_id
    
    console.log(shop_id)
    network.postRequest('recruitment/info/create',{
      product_id: that.data.RecruitId,
      industryName: industry_val,
      vocation: job_val,
      obligation: obligation,
      require: tenure,
      number: people,
      min_money: min_money,
      max_money: max_money,
      min_age: min_age,
      max_age: max_age,
      welfare: welfare_val,
      shop_id: shop_id
    },function(res){//请求成功跳转招聘列表
      console.log(res)
      wx.navigateTo({
        url: '/pages/index/index?currentTab=' + 2,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    })
  }

})

function getRecruitData(that, recruit_id){
  //选择行业添加其他
  let shop_id = network.shopParams().shop_id
  network.getRequest('recruitment/info/create?id=' + recruit_id, {}, function (res) {
    console.log(res)
    let res_detail = res.detail
    let welfare_arr = JSON.parse(res_detail.welfare)//将字符串转为数组
    console.log(welfare_arr)
    if (welfare_arr.length > 0) {//如果福利大于0，则显示
      that.setData({
        choose_welfare_arr: welfare_arr
      })
    }
    that.setData({
      industry_val: res_detail.industryName,
      job_val: res_detail.vocation,
      duty_val: res_detail.obligation,
      qualifications_val: res_detail.require,
      peoples: res_detail.number,
      min_money: res_detail.min_money / 100,
      max_money: res_detail.max_money / 100,
      welfares: welfare_arr,
      min_age: res_detail.min_age,
      max_age: res_detail.max_age,
      icon_industry: false,
      icon_job: false,
      icon_zhize: false,
      icon_zige: false,
      icon_people: false,
      icon_min_money: false,
      icon_max_money: false,
      icon_fuli: false,
      icon_min_age: false,
      icon_max_age: false,
    })
  })
}